package com.guoning.office_system.service.impl;

import com.guoning.office_system.mapper.FunctionMapper;
import com.guoning.office_system.model.Function;
import com.guoning.office_system.service.FunctionService;
import com.guoning.office_system.vo.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class FunctionServiceImpl implements FunctionService {

    @Autowired
    FunctionMapper functionMapper;

    @Override
    public List getMenuList(Long userId) {
        Criteria criteria = new Criteria();
        criteria.addExtField("userId", userId);
        List<Map> menuList = functionMapper.getMenuListByUserId(criteria);
        List<Map> list = this.setMenuChildren(menuList, criteria, null);
        return list;
    }

    private List<Map> setMenuChildren(List<Map> list, Criteria criteria, String str){
        if(list.size() == 0){
            return list;
        }
        String minuId;
        for (int i=0; i<list.size(); i++){
            criteria.addExtField("pid", list.get(i).get("id").toString());
            List<Map> menuList1 = functionMapper.getMenuByFuncId(criteria);
            if(str != null){
                minuId = str + "-" + (i+1);
            }else{
                minuId = String.valueOf(i+1);
            }
            if (menuList1.size() != 0){
                this.setMenuChildren(menuList1, criteria, minuId);
                list.get(i).put("menus", menuList1);
                list.get(i).put("hasChild", true);
            }else {
                list.get(i).put("hasChild", false);
            }
            list.get(i).put("menuId", minuId);
        }
        return list;
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return 0;
    }

    @Override
    public int insert(Function record) {
        return 0;
    }

    @Override
    public int insertSelective(Function record) {
        return 0;
    }

    @Override
    public Function selectByPrimaryKey(Long id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(Function record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Function record) {
        return 0;
    }
}